
import userStore, { useUserPermission } from '../store/userStore';
export function hasPermission(permission) {
  const permissions = userStore.getState().userInfo.permissions || [];
  return permissions.includes(permission);
}

// 自定义Hook，用于检查权限
function useAuth(permission) {
  const permissions = useUserPermission();
  const isAllowed = hasPermission(permissions, permission);
  return { isAllowed };
}

// 高阶组件，用于权限控制
const withAuth = (WrappedComponent, permission) => {
  return class extends React.Component {
    render() {
      const { userRole, ...rest } = this.props;
      const isAllowed = hasPermission(userRole, permission);

      if (!isAllowed) {
        // 根据需要返回null，或者重定向，或者渲染一个无权限的提示组件等
        return null; // 或者 <Redirect to="/unauthorized" /> 等
      }

      return <WrappedComponent {...rest} />;
    }
  };
};